package com.github.pig.admin.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pig.admin.model.dto.ChannelSourceTree;
import com.github.pig.admin.model.dto.LabelTree;
import com.github.pig.admin.model.entity.ChannelSource;
import com.github.pig.admin.model.entity.SysLabel;
import com.github.pig.admin.service.SysLabelService;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/label")
public class LabelController extends BaseController {

    @Autowired
    private SysLabelService service;

    /**
     * 返回树形菜单集合
     *
     * @return 树形菜单
     */
    @GetMapping(value = "/tree")
    public JSONArray getTree() {
        SysLabel condition = new SysLabel();
        condition.setDelFlag(Integer.parseInt(CommonConstant.STATUS_NORMAL));
        List<LabelTree> trees = service.selectListTree(new EntityWrapper<SysLabel>(condition));
        String treeStr = JSONArray.toJSONString(trees);
        treeStr = treeStr.replaceAll("\"children\":\\[\\],", "");
        return JSONArray.parseArray(treeStr);
    }


    /**
     * 获取用户组织路径
     * @param id 标签ID
     * @return 用户组织路径列表
     */
    @GetMapping(value = "/getObj/{id}")
    public SysLabel getObj(@PathVariable Integer id) {
        return service.selectById(id);
    }

    @PostMapping
    public Boolean addObj(@RequestBody SysLabel sysLabel){
        sysLabel.setCreateTime(new Date());
        sysLabel.setUpdateTime(new Date());
        sysLabel.setDelFlag(Integer.parseInt(CommonConstant.STATUS_NORMAL));
        return service.insert(sysLabel);
    }

    /**
     * 修改
     * @param sysLabel
     * @return
     */
    @PutMapping
    public Boolean edit(@RequestBody SysLabel sysLabel){
        sysLabel.setUpdateTime(new Date());
        return service.updateById(sysLabel);
    }


    /**
     * 删除
     * @param sysLabel
     * @return
     */
    @DeleteMapping
    public Boolean remove(@RequestBody SysLabel sysLabel){
        sysLabel.setDelFlag(Integer.parseInt(CommonConstant.STATUS_DEL));
        return service.updateById(sysLabel);
    }

    @GetMapping("/getAll")
    public List<SysLabel> getAll(){
        SysLabel sysLabel = new SysLabel();
        sysLabel.setDelFlag(Integer.parseInt(CommonConstant.STATUS_NORMAL));
        return service.selectList(new EntityWrapper<SysLabel>(sysLabel));
    }


}
